/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cz.zcu.fav.ratdroid.activity.calendar.day;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.widget.Button;

import cz.zcu.fav.ratdroid.R;
import cz.zcu.fav.ratdroid.bean.Period;
import cz.zcu.fav.ratdroid.bean.PickAction;
import cz.zcu.fav.ratdroid.bean.ReturnAction;
import cz.zcu.fav.ratdroid.bean.User;

/**
 * Custom view that represents a {@link Blocks#BLOCK_ID} instance, including its
 * title and time span that it occupies. Usually organized automatically by
 * {@link BlocksLayout} to match up against a {@link TimeRulerView} instance.
 */
public class BlockView extends Button
{
	private static final int DISABLED_BLOCK_ALPHA = 100;

	private final String title;
	private final long startTime;
	private final long endTime;
	private final int column;
	private final Period period;

	public Period getPeriod()
	{
		return period;
	}

	public BlockView(Context context, String title, int column, Period period, LocalDate selectedDate)
	{
		super(context);

		this.title = title;
		this.startTime = period.getFromDate().toLocalDate()
				.isBefore(selectedDate) ? selectedDate.toDateTimeAtStartOfDay()
				.getMillis() : period.getFromDate().toDateTime().getMillis();

		this.endTime = period.getToDate().toLocalDate()
				.isAfter(selectedDate) ? selectedDate.plusDays(1)
				.toDateTimeAtStartOfDay().minusMinutes(1).getMillis() : period
				.getToDate().toDateTime().getMillis();

		this.column = column;
		this.period = period;
		setText(this.title);

		int textColor = Color.BLACK;

		LayerDrawable buttonDrawable = (LayerDrawable) context.getResources()
				.getDrawable(R.drawable.btn_block);

		buttonDrawable.getDrawable(0)
				.setColorFilter(evaluateColor(buttonDrawable, period),
						PorterDuff.Mode.SRC_ATOP);

		if (period.getFromDate().isBefore(LocalDateTime.now())
				&& period.getToDate().isBefore(LocalDateTime.now()))
		{
			buttonDrawable.getDrawable(0).setAlpha(DISABLED_BLOCK_ALPHA);

		}
		buttonDrawable.getDrawable(2).setAlpha(
				period.getReservation().isPeriodical() ? 255 : 0);

		setTextColor(textColor);
		setBackground(buttonDrawable);
	}

	protected int evaluateColor(LayerDrawable buttonDrawable, Period period)
	{
		int accentColor = -1;

		if (!period.isValid())
		{
			accentColor = getResources().getColor(R.color.block_color_grey);
		} else
		{

			if (period.getFromDate().isBefore(LocalDateTime.now()))
			{
				if (period.getToDate().isBefore(LocalDateTime.now()))
				{
					// past
					if (period.getReturnAction() == ReturnAction.NOT_RETURNED)
					{
						accentColor = getResources().getColor(
								R.color.block_color_red);
					} else
					{
						accentColor = getResources().getColor(
								R.color.block_color_grey);
					}
				} else
				{
					// now
					if (period.getPickAction() == PickAction.NOT_PICKED)
					{
						accentColor = getResources().getColor(
								R.color.block_color_red);
					} else
					{
						if (period.getReturnAction() == ReturnAction.RETURNED)
						{
							accentColor = getResources().getColor(
									R.color.block_color_grey);
						} else if (period.getReturnAction() == ReturnAction.NOT_RETURNABLE)
						{
							accentColor = getResources().getColor(
									R.color.block_color_yellow);
						} else
						{
							accentColor = getResources().getColor(
									R.color.block_color_green);
						}

					}
				}

			} else
			{
				// future
				if (period.getPickAction() == PickAction.NOT_PICKED)
				{
					accentColor = getResources().getColor(
							R.color.block_color_blue);
				} else
				{
					accentColor = getResources().getColor(
							R.color.block_color_yellow);
				}
			}

		}

		return accentColor;
	}

	public long getStartTime()
	{
		return startTime;
	}

	public long getEndTime()
	{
		return endTime;
	}

	public int getColumn()
	{
		return column;
	}

	public boolean isEditable()
	{
		return period.getFromDate().isAfter((LocalDateTime.now()));
	}

	public boolean isCurrentUserReservation(Context context)
	{
		Integer userId = period.getReservation().getUserId();
		int loggedUserId = context.getSharedPreferences(
				User.SINGLE_USER_IDENTIFIER, Context.MODE_PRIVATE).getInt(
				User.USER_ID, -1);
		return (userId.intValue() == loggedUserId);
	}
}
